home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Special 25 / AMIGAplus Sonderheft 25 (2000)(Falke)(DE)(Track 1 of 4)[!].iso / Updates / HD-Installer / jst_dev / sources / src / DiskTools / rippsyfiles.c < prev    next >
C/C++ Source or Header  |  2000-04-12  |  4KB  |  208 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <signal.h>
  4.  
  5. #include <dos/dos.h>
  6. #include <proto/dos.h>
  7. #include <proto/exec.h>
  8. #include <exec/execbase.h>
  9. #include <exec/libraries.h>
  10. #include <exec/memory.h>
  11. #include <pragmas/dos_pragmas.h>
  12. #include <string.h>
  13.  
  14. #define RAWTRACK_LEN 0x8000
  15. #define TRACK_LEN 0x1800
  16. #define START_TRACK 2
  17. #define END_TRACK 159
  18. #define NB_TRACKS END_TRACK-START_TRACK+1
  19.  
  20. extern struct ExecBase *SysBase;
  21.  
  22. extern APTR InitTrackDisk(ULONG);
  23. extern void ShutTrackDisk(void);
  24. extern ULONG CheckDiskIn (void);
  25.  
  26. extern ULONG ReadRawTrackIndex(ULONG,UBYTE *);
  27. extern ULONG DecodeTrack(UBYTE *, UBYTE *);
  28. extern ULONG WriteFiles(UBYTE *);
  29.  
  30. UBYTE *rawTrackBuffer=0L;
  31. UBYTE *decTrackBuffer=0L;
  32. char *charunit="DF0:";
  33. int allocated=0,diskinit=0;
  34. BPTR oldlock,newlock;
  35.  
  36.  
  37. void CloseAll(char *mess)
  38.  
  39. {
  40. if (mess) printf("** %s\n",mess);
  41.  
  42. if (diskinit) ShutTrackDisk();
  43. if (allocated) Inhibit(charunit,FALSE);
  44. if (rawTrackBuffer) FreeMem((APTR)rawTrackBuffer,RAWTRACK_LEN);
  45. if (decTrackBuffer) FreeMem((APTR)decTrackBuffer,TRACK_LEN*NB_TRACKS);
  46.  
  47. if (newlock)
  48.   {
  49.   CurrentDir(oldlock);
  50.   UnLock(newlock);
  51.   }
  52.  
  53. exit(0);
  54. }
  55.  
  56. void BreakHandle(int code)
  57.  
  58. {
  59. CloseAll("User break");
  60. }
  61.  
  62.  
  63. main(unsigned int argc,char ** argv)
  64.  
  65.  
  66. {
  67.   ULONG diskunit,numid,readid;
  68.   char strid[10];
  69.   int i,readok,retries,dcrslt;
  70.   char *trkptr;
  71.  
  72.   printf("RipPsyFiles V2.1, the Psygnosis disk file ripper by Jean-François Fabre\n");
  73.  
  74.   switch(argc)
  75.    {
  76.    case 0:
  77.    case 1:
  78.    case 2:
  79.     printf("Usage : rippsyfiles <unit number> <destination-dir> [<diskid>]\n");
  80.     if (!argc) Delay(200);
  81.     exit(0);
  82.  
  83.    default:
  84.     /* Try to lock the specified directory */
  85.  
  86.     newlock = Lock(argv[2],ACCESS_READ);
  87.     if (!newlock) CloseAll("Couldn't lock destination directory");
  88.  
  89.     /* Save current directory lock */
  90.     /* And change directory */
  91.  
  92.     oldlock = CurrentDir(newlock);
  93.     break;
  94.    }
  95.  
  96.   signal(SIGINT,BreakHandle);
  97.   
  98.   diskunit=(ULONG)argv[1][0]-'0';
  99.  
  100.   if ((diskunit>3)||(diskunit<0)) {printf("** Unit %d unavailable\n",diskunit);CloseAll(0);}
  101.  
  102.   charunit[2]=diskunit+'0';
  103.  
  104.   if (SysBase->LibNode.lib_Version>36)
  105.     {
  106.     if(Inhibit(charunit,DOSTRUE)==FALSE) CloseAll("Can't allocate device !");
  107.     allocated=1;
  108.     }
  109.  
  110.   if (argc<4) numid=0; /* no ID check */
  111.  
  112.   else
  113.      {
  114.      strncpy(strid,argv[3],8);
  115.      switch(strid[0])
  116.     {
  117.     case '\'':
  118.         numid=(((int)(strid[1]))<<24)+(((int)(strid[2]))<<16)+
  119.               (((int)(strid[3]))<<8)+((int)(strid[4]));
  120.         break;
  121.  
  122.     default: sscanf(strid,"%x",&numid);break;
  123.  
  124.     }
  125.      }
  126.  
  127.   if ((ULONG)InitTrackDisk(diskunit)<0) {printf("** Can't open unit %d !",diskunit);CloseAll(0);}
  128.   diskinit=1;
  129.   if (CheckDiskIn()) {printf("** No disk in unit %d !\n",diskunit);CloseAll(NULL);}
  130.  
  131.   decTrackBuffer=(char *)AllocMem(TRACK_LEN*NB_TRACKS,MEMF_CLEAR);
  132.   if (decTrackBuffer==0L) CloseAll("Can't allocate memory.");
  133.  
  134.   rawTrackBuffer=(char *)AllocMem(RAWTRACK_LEN,MEMF_CHIP|MEMF_CLEAR);
  135.   if (rawTrackBuffer==0L) CloseAll("Can't allocate chip memory.");
  136.  
  137.   trkptr=decTrackBuffer;
  138.  
  139.   printf("Reading disk data...\n");
  140.  
  141.   for (i=START_TRACK;i<END_TRACK+1;i++)
  142.     {
  143.     retries=0;
  144.     readok=0;
  145.  
  146.         do
  147.         {
  148.              if (ReadRawTrackIndex(i,rawTrackBuffer))
  149.             {
  150.                 if (retries>2) CloseAll("Low level disk read error!");
  151.             }
  152.  
  153.              else
  154.              {
  155.                  dcrslt=DecodeTrack(rawTrackBuffer,trkptr);
  156.  
  157.                  if (!dcrslt)
  158.                     {
  159.                         if (i==2)
  160.                         {
  161.                             /* disk ID check if specified */
  162.  
  163.                             readid=(((int)(trkptr[0x10]))<<24)+(((int)(trkptr[1+0x10]))<<16)+
  164.                                   (((int)(trkptr[2+0x10]))<<8)+((int)(trkptr[3+0x10]));
  165.  
  166.                             if (numid)
  167.                                 {
  168.                                  if (readid!=numid) 
  169.                                     CloseAll("Disk ID does not match");
  170.                                 }
  171.                             else printf("DiskID : %X\n",readid);
  172.                         }
  173.  
  174.                         readok=1;
  175.                         trkptr+=TRACK_LEN;
  176.                     }
  177.                 else
  178.                     {
  179.                         if (retries>2)
  180.  
  181.                             switch(dcrslt)
  182.                             {    
  183.                             case -1: printf("** Error Track %d\n",i);CloseAll("Decode disk read error!");
  184.                             default: printf("** Error Track %d\n",i);CloseAll("No sync found!");
  185.                             }
  186.                     }
  187.  
  188.              }
  189.  
  190.     retries++;
  191.     }
  192.     while (!readok);
  193.  
  194.  
  195.     }
  196.  
  197.     switch(WriteFiles(decTrackBuffer))
  198.  
  199.     {
  200.     case 0 : printf("Disk read successfuly!\n");CloseAll(NULL);
  201.     case -1: CloseAll("File Creation Error");
  202.     case -3: CloseAll("Directory Structure Error");
  203.     case -5: CloseAll("Memory Allocation Error");
  204.     default: CloseAll("Unknown error");
  205.     }
  206.  
  207. }
  208.